﻿using System.Windows;
using System.Windows.Controls;
using ShadeTree.Binding.Elements;

namespace ShadeTree.Binding.WPF
{
    public class LabelElement : BoundScreenElement<Label, object>, ITextEditingElement
    {
        private FormatValue _format = o => o.ToString();
        private CoerceFunction _coercion;

        public LabelElement(Accessor accessor, Label control) : base(accessor, control)
        {
            _coercion = ((p, rawValue) => CoercionUtility.Coerce(p, rawValue));

            if (CoercionUtility.IsNumeric(accessor.PropertyType))
            {
                control.HorizontalAlignment = HorizontalAlignment.Right;
            }
        }


        public FormatValue Format
        {
            get { return _format; }
            set { _format = value; }
        }

        public CoerceFunction Coercion
        {
            get { return _coercion; }
            set { _coercion = value; }
        }

        protected override object getValueFromControl()
        {
            return BoundControl.Content;
        }

        protected override void resetControl(object originalValue)
        {
            if (originalValue == null)
            {
                BoundControl.Content = string.Empty;
                return;
            }

            BoundControl.Content = originalValue;
        }

        protected override void tearDown()
        {
        }
    }
}